package com.yoso.shop.application.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSON;
import com.yoso.base.service.BaseService;
import com.yoso.shop.application.service.ICartService;
import com.yoso.shop.application.service.ICommodityService;
import com.yoso.shop.domain.cart.CartEntity;
import com.yoso.shop.domain.cart.CartEntityExample;
import com.yoso.shop.domain.commodity.CommodityEntityExample;
import com.yoso.shop.infrastructure.factory.ShopBeanFactory;
import com.yoso.shop.interfaces.vo.cart.CartVo;
import com.yoso.shop.interfaces.vo.commodity.CommodityVo;
import com.yoso.shop.interfaces.vo.order.OrderVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author shizejin
 * @date 2022/1/30
 * @description:
 */
@Service
@Slf4j
public class CartServiceImpl extends BaseService<CartEntity, CartVo, CartEntityExample> implements ICartService {
    @Autowired
    private ICommodityService commodityService;

    @Override
    public CartEntity V2E(CartVo data) {
        return JSON.parseObject(JSON.toJSONString(data), CartEntity.class);
    }

    @Override
    public CartVo E2V(CartEntity data) {
        return JSON.parseObject(JSON.toJSONString(data), CartVo.class);

    }

    @Override
    public CartEntityExample getEntityExample(Map<String, String> paramMap) {
        return null;
    }

    @Override
    public void flushCart(OrderVo orderVo) {
        CartVo cartVo = ShopBeanFactory.buildCartVo(orderVo);
        this.deleteByUniqueKey(cartVo);
    }

    @Override
    public int deleteByUniqueKey(CartVo cartVo) {
        CartEntityExample example = new CartEntityExample();
        example.createCriteria()
                .andBuyerIdEqualTo(cartVo.getBuyerId())
                .andCommodityCodeEqualTo(cartVo.getCommodityCode())
                .andColorEqualTo(cartVo.getColor())
                .andSizeEqualTo(cartVo.getSize());
        return this.deleteByExample(example);
    }

    /**
     * 根据用户账号获得购物车列表
     */
    @Override
    public List<CartVo> getCartByUsername(String username) {
        CartEntityExample example = new CartEntityExample();
        example.createCriteria()
                .andBuyerIdEqualTo(username);
        List<CartVo> cartVos = this.selectByExample(example);

        if (CollUtil.isEmpty(cartVos)) {
            return CollUtil.newArrayList();
        }
        return this.buildDetailInfoList(cartVos);

    }

    /**
     * 构建购物车详情（添加商品信息）
     */
    @Override
    public List<CartVo> buildDetailInfoList(Iterable<CartVo> cartVos) {
        List<CartVo> result = CollUtil.newArrayList();
//        购物车中的商品代码
        List<String> codeList = CollUtil.newArrayList();
        for (CartVo cartVo : cartVos) {
            codeList.add(cartVo.getCommodityCode());
        }

        CommodityEntityExample commodityExample = new CommodityEntityExample();
        commodityExample.createCriteria()
                .andCodeIn(codeList);

        List<CommodityVo> commodityVos = commodityService.selectByExample(commodityExample);

        List<CommodityVo> detailInfoList = commodityService.buildDetailInfoList(commodityVos);
        Map<String, CommodityVo> code2VoMap = detailInfoList
                .parallelStream().collect(Collectors.toMap(
                        CommodityVo::getCode,
                        Function.identity(),
                        (v1, v2) -> v2
                ));
        for (CartVo cartVo : cartVos) {
            String code = cartVo.getCommodityCode();
            cartVo.setCommodity(code2VoMap.get(code));
            result.add(cartVo);
        }
        return result;
    }
}
